home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / lang / AmigaTalk.lha / general / List.st < prev    next >
Text File  |  2002-03-24  |  2KB  |  109 lines

  1. "-------------------------------------------------------"
  2. "  Lists are implemented using Points in order to       "
  3. "  reduce the number of classes in the standard prelude "
  4. "-------------------------------------------------------"
  5.  
  6. Class List :SequenceableCollection ! first current !
  7. [
  8.    add: anItem
  9.       first <- (Point new x: anItem ) y: first.
  10.       ^ anItem
  11. |
  12.    addFirst: anItem
  13.       first <- (Point new x: anItem ) y: first.
  14.       ^ anItem
  15. |
  16.    addLast: anItem
  17.       (first isNil) 
  18.          ifTrue: [^ self addFirst: anItem].
  19.  
  20.       (self findLast) y: ((Point new x: anItem) y: nil).
  21.  
  22.       ^ anItem
  23. |
  24.    addAllFirst: aCollection
  25.       aCollection do: [:x | self addFirst: x]
  26. |   
  27.    addAllLast: aCollection
  28.       aCollection do: [:x | self addLast: x]
  29. |
  30.    coerce: aCollection    ! newList !
  31.       newList <- List new.
  32.  
  33.       aCollection do: [:x | newList addLast: x].
  34.  
  35.       ^ newList
  36. |
  37.    findLast     ! item !
  38.       ((item <- first) isNil)
  39.          ifTrue: [^ nil].
  40.  
  41.       [(item y) notNil]
  42.          whileTrue: [item <- item y].
  43.  
  44.       ^ item
  45. |
  46.    remove: anItem
  47.       ^ self remove: anItem 
  48.            ifAbsent: [self error: 'cant find item']
  49. |
  50.    remove: anItem ifAbsent: exceptionBlock
  51.       (first isNil) 
  52.          ifTrue: [^ exceptionBlock value].
  53.  
  54.       self inject: nil 
  55.              into: [:prev :current |
  56.  
  57.                      (current x == anItem)
  58.                      ifTrue: [(prev isNil)
  59.                               ifTrue:  [first <- current y]
  60.                               ifFalse: [prev y: (current y)].
  61.          
  62.                               ^ anItem
  63.                              ].
  64.       
  65.                      current 
  66.                    ].
  67.       
  68.       ^ exceptionBlock value
  69. |
  70.    removeError
  71.       ^ self error: 'cannot remove from an empty list'
  72. |
  73.    removeFirst      ! item !
  74.       (first isNil)
  75.          ifTrue: [^ self removeError].
  76.  
  77.       item  <- first.
  78.       first <- first y.
  79.  
  80.       ^ item x
  81. |
  82.    removeLast
  83.       (first isNil)
  84.          ifTrue: [^ self removeError].
  85.  
  86.       ^ self remove: self last 
  87.            ifAbsent: [self removeError]
  88. |
  89.    first
  90.       ^ ((current <- first) notNil) 
  91.          ifTrue: [ current x ]
  92. |
  93.    next
  94.       ^ ((current <- current y) notNil)
  95.          ifTrue: [ current x ]
  96. |
  97.    current
  98.       ^ current x
  99. |
  100.    last
  101.       (first isNil) 
  102.          ifTrue: [^ nil].
  103.   
  104.       ^ self findLast x
  105. |
  106.    isEmpty
  107.       ^ first == nil
  108. ]
  109.